package kr.hyunist.androidcomplete01saeildamil.ch12;

import java.util.*;

import kr.hyunist.androidcomplete01saeildamil.*;
import android.app.*;
import android.content.*;
import android.content.DialogInterface.OnClickListener;
import android.os.*;
import android.view.*;
import android.widget.*;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;

public class CustomAdapterEx3 extends Activity {
	ArrayList<AlarmInfo> arList;
	AlarmInfo info;
	ListView list;
	AlarmAdapter adapter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.customadapterex);
		
		arList = new ArrayList<AlarmInfo>();
		info = new AlarmInfo("02:00", R.drawable.dark, false);
		arList.add(info);
		info = new AlarmInfo("07:10", R.drawable.normal, true);
		arList.add(info);
		info = new AlarmInfo("07:00", R.drawable.normal, true);
		arList.add(info);
		info = new AlarmInfo("06:30", R.drawable.normal, true);
		arList.add(info);
		
		adapter = new AlarmAdapter(CustomAdapterEx3.this, arList);
		
		list = (ListView) findViewById(R.id.customeadapterexlistview);
		list.setAdapter(adapter);
		list.setOnItemClickListener(mClickListener);
		list.setOnItemLongClickListener(mLongClickListener);
		
	}
	
	OnItemClickListener mClickListener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
			boolean enable = arList.get(position).isEnable();
			arList.get(position).setEnable(!enable);
			adapter.notifyDataSetChanged();
		}
		
	};
	
	OnItemLongClickListener mLongClickListener = new OnItemLongClickListener() {

		@Override
		public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
			final int pos = position;
			
			AlertDialog.Builder dlg = new AlertDialog.Builder(CustomAdapterEx3.this);
			dlg.setTitle("삭제 진짜 하심?");
			dlg.setIcon(R.drawable.ic_launcher);
			dlg.setPositiveButton("확인", new OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					arList.remove(pos);
					adapter.notifyDataSetChanged();
				}
			});
			dlg.setNegativeButton("취소", new OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					
				}
				
			});
			dlg.show();
			
			return true;
		}
		
	};
}

class AlarmInfo {
	private String time;
	private int background;
	private boolean enable;

	public AlarmInfo(String time, int background, boolean enable) {
		super();
		this.time = time;
		this.background = background;
		this.enable = enable;
	}

	public String getTime() {
		return time;
	}

	public int getBackground() {
		return background;
	}

	public boolean isEnable() {
		return enable;
	}

	public void setTime(String time) {
		this.time = time;
	}

	public void setBackground(int background) {
		this.background = background;
	}

	public void setEnable(boolean enable) {
		this.enable = enable;
	}
	
}

